package com.cloudcentral.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.zaxxer.hikari.HikariDataSource;
import io.seata.rm.datasource.DataSourceProxy;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.sql.DataSource;

/**
 * Create by WEI
 * 2023/10/11 09:51
 */
@Slf4j
@Configuration
@EnableConfigurationProperties({RedisProperties.class})
public class CloudCentralConfiguration implements WebMvcConfigurer {
  private final RedisProperties redisProperties;
  private final DataSourceProperties dataSourceProperties;

  @Autowired
  public CloudCentralConfiguration(RedisProperties redisProperties, DataSourceProperties dataSourceProperties) {
    this.redisProperties = redisProperties;
    this.dataSourceProperties = dataSourceProperties;
  }

  @Bean
  public OpenAPI swaggerInfo(@Value("${springdoc.doc-name}") String docName) {
    Contact contact = new Contact();
    contact.setName("魏威");
    contact.setEmail("1536610606@qq.com");
    return new OpenAPI().info(new Info().title("超视距研究所云服务中心").description(docName + "接口文档说明").contact(contact).version("v1.0"));
  }

  @Bean
  public DataSource dataSourceProxy() {
    HikariDataSource hikariDataSource = new HikariDataSource();
    hikariDataSource.setDriverClassName(dataSourceProperties.getDriverClassName());
    hikariDataSource.setJdbcUrl(dataSourceProperties.getUrl());
    hikariDataSource.setUsername(dataSourceProperties.getUsername());
    hikariDataSource.setPassword(dataSourceProperties.getPassword());
    return new DataSourceProxy(hikariDataSource);
  }

  // 创建事务管理器
  @Bean
  public PlatformTransactionManager transactionManager(@Qualifier("dataSource") DataSource dataSource) {
    return new DataSourceTransactionManager(dataSource);
  }

  @Bean
  public MybatisPlusInterceptor mybatisPlusInterceptor() {
    MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
    return interceptor;
  }

  @Bean
  public RedissonClient redissonClient() {
    Config config = new Config();
    config.useSingleServer()
        .setAddress("redis://" + redisProperties.getHost() + ":" + redisProperties.getPort())
        .setTimeout((int) redisProperties.getTimeout().toMillis())
        .setDatabase(redisProperties.getDatabase())
        .setUsername(redisProperties.getUsername())
        .setPassword(redisProperties.getPassword());
    return Redisson.create(config);
  }
}
